<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form layui-form-pane" action="" name="frmMain">
    <input type="hidden" name="mid">
    <div class="mainBox">
        <div id="role"></div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
        </div>
    </div>
</form>
<script src="../../../config/apiUrl.js"></script>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    async function init(){
        let id = layui.common.getUrlParams("id");
        if(id == null){
            layui.toast.error({title: false, message: "请选中一个用户"})
        }else{
            frmMain.mid.valur = id;
            //查询当前用户对于的角色
            let user_role_list = await layui.common.ajax({
                url:apiUrlConfig.role.getByMidSysRoleList+id,
                type:"get"
            })
            //角色列表
            let role_list = await layui.common.ajax({
                url:apiUrlConfig.role.List
            })
            for (const roleListElement of role_list.data) {
                roleListElement.spread = true;
            }
            let role_tree = layui.common.arrToTreeArray(role_list.data,"pid",0)
            // 渲染
            layui.tree.render({
                elem: '#role',
                data: role_tree,
                showCheckbox: true,  // 是否显示复选框
                onlyIconControl: true,  // 是否仅允许节点左侧图标控制展开收缩
                id: 'role',
                isJump: true, // 是否允许点击节点时弹出新窗口跳转
                click: function (obj) {
                    var data = obj.data;  //获取当前点击的节点数据
                },
                customName: { // 自定义 data 字段名 --- 2.8.14+
                    id: 'id',
                    title: 'role_name',
                    children: 'children'
                }
            });
            for (const datum of user_role_list.data) {
                layui.tree.setChecked('role', [datum.id]);
            }
        }
    }
    function closeWin() {
        let layer_index = parent.layer.getFrameIndex(window.name);
        if (layer_index > 0) {
            parent.layer.close(layer_index); //关闭当前页
        }
    }
    async function savePower(data){
        try {
            let id = layui.common.getUrlParams("id");
            let res_data = await layui.common.ajax({
                url:apiUrlConfig.userMapping.savePowerRole+id,
                data:JSON.stringify(data)
            })
            parent.layui.toast.success({title: false,message: '保存成功'})
            parent.layui.table.reloadData('userList', {}, true)
            closeWin();
        }catch (e){
        }
    }
    layui.use(['tree','form', 'jquery', 'http', 'popup', 'common', 'toast'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        var tree = layui.tree;
        init();
        $(document).keyup(function (event) {
            if (event.keyCode == 27) {
                closeWin();
            }
        });

        form.on('submit(user-save)', function (data) {
            var checkData = tree.getChecked('role');
            var ck_arr = common.TreeToArray(checkData);
            savePower(ck_arr);
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>
